# -*- coding: utf-8 -*-

from sql import user_password,user_info_by_openid,u_info_by_openid,bind_openid,select_schoollist,userunbind_tool,user_apiflage
from web.views.base import get_cache, set_cache
from hashlib import md5
from datetime import datetime
import time
from random import randint
from setting import WEAPP_ID, WEAPP_SECRET
import requests
import json

from  QRcodecelery import send_notify_template_by_openid
class QRServer(object):

    @classmethod
    def verify_user_password_open(cls, uid, passwd, cop_id, open_id): #用户绑定
        user_api=user_apiflage(cop_id) #通过cop_id 查询 用户帮定时 验证的方式 如果存在 api 验证 则采用 api 验证
        if user_api[0]==True: # True 调用 api 验证
            data={
                'uid':uid,
                'passwd':passwd
            }
            res = requests.post (str(user_api[1]), data=data, verify=False) #调用 api 验证
            if res.status_code==200:
                print "1231231232113"
                res=res.json()
                if res['errorcode']==0:
                    param={
                        'uid':uid,
                        'passwd':passwd,
                        'cop_id':cop_id,
                        'open_id':open_id,
                        'cop_name':user_api[2],
                        'department':res.get('data').get('department'),
                        'class_':res.get('data').get('class_'),
                        'major':res.get('data').get('major'),
                        'name':res.get('data').get('name')
                    }
                    #uid,cop_id,open_id,passwd,cop_name,name
                    bind_openid (**param) #将用户的 openid cop_id open_id 写入数据库
                    return 0 #
                else:
                    return 2
            else:
                return 2
        else:
            if user_password(uid,passwd,cop_id):
                bind_openid(uid,cop_id,open_id)
            # send_notify_template_by_openid(open_id)
                return 0
            return 2

    @classmethod
    def verify_user_password_md5(cls, uid, passwd, cop_id, open_id):
        if user_password(uid, md5(passwd).hexdigest(), cop_id):
            bind_openid(uid, cop_id, open_id)
            return 0
        return 2


    @classmethod
    def code_to_user(cls,code):
        print '2222'
        print code
        user_info = get_cache(code)


        return user_info


    @classmethod
    def get_code(cls,open_id):
        code = md5(str(time.time())+str(open_id)+str(randint(1,100))).hexdigest()
        user = user_info_by_openid(open_id)
        if user:
            set_cache(code,{'uid':user[0]},1)
            return code
        else:
            return 'unbind'

    @classmethod
    def get_openid(cls,js_code):
        url = 'https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code'.format(WEAPP_ID,WEAPP_SECRET,js_code)
        res = requests.get(url)
        res = json.loads(res.content)
        return {'open_id':res.get('openid',None),'session_key':res.get('session_key',None)}


    @classmethod
    def get_user_info(cls,open_id):
        data = u_info_by_openid(open_id)
        if data:
            return {
                'userid':data[0],
                'cop_name':data[1],
                'name':data[2],
                'cop_id':data[3]
            }
        else:
            return 'unbind'

    @classmethod
    def get_schoollist(cls, open_id):
        data=select_schoollist(open_id)
        if data:
            return data

    @classmethod
    def user_unbind(cls, uid, cop_id, open_id): #用户解绑
        print uid,cop_id,open_id
        if userunbind_tool(uid, cop_id, open_id):
            return 0
        return 2























